package com.keke.myblogv1.users.controller;

import com.keke.myblogv1.common.result.ResponseResult;
import com.keke.myblogv1.users.entity.MyBlogUsers;
import com.keke.myblogv1.users.service.MyBlogUsersService;
import com.keke.myblogv1.users.vo.UserLoginVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Derrick
 * @since 2022-08-02
 */
@RestController
@RequestMapping("/users")
public class MyBlogUsersController {

    @Resource
    private MyBlogUsersService myBlogUsersService;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ResponseResult<UserLoginVO> login(@RequestBody MyBlogUsers myBlogUsers){
        UserLoginVO userLoginVO = null;
        try{
            userLoginVO = myBlogUsersService.getLoginUser(myBlogUsers);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error();
        }
        return ResponseResult.success(userLoginVO);
    }
    @RequestMapping(value = "/info",method = RequestMethod.GET)
    public ResponseResult<MyBlogUsers> getUserInfo(@RequestParam("token") String token){
        MyBlogUsers myBlogUsers = null;
        try{
            myBlogUsers = myBlogUsersService.getUserInfo(token);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error();
        }
        return ResponseResult.success(myBlogUsers);
    }

    @RequestMapping(value = "/logout",method = RequestMethod.POST)
    public ResponseResult logout(){
        try{
            myBlogUsersService.logout();
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error();
        }
        return ResponseResult.success();
    }
}
